package network.tcp;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.Socket;
import java.util.Scanner;

public class TcpEchoClient {
    private final Socket socket;
    public TcpEchoClient(String serverIp, int serverPort) throws IOException {
        socket = new Socket(serverIp, serverPort);
    }
    public void start() {
        System.out.println("客户端，启动！");
        Scanner scannerConsoleIn = new Scanner(System.in);
        try(InputStream inputStream = socket.getInputStream();
            OutputStream outputStream = socket.getOutputStream()) {
            Scanner scannerNetWork = new Scanner(inputStream);
            PrintWriter printWriter = new PrintWriter(outputStream);
            while(true) {
                System.out.print(">> ");
                String request = scannerConsoleIn.nextLine();
                printWriter.println(request);
                printWriter.flush();
                String response = scannerNetWork.nextLine();
                System.out.println(response);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) throws IOException {
        TcpEchoClient client = new TcpEchoClient("127.0.0.1", 9091);
//        TcpEchoClient client = new TcpEchoClient("62.234.27.59", 9091);
//        TcpEchoClient client = new TcpEchoClient("10.251.193.179", 9091);
        //TcpEchoClient client = new TcpEchoClient("fe80::437e:a99f:edaf:7742%5", 9091);
        client.start();
    }
}
